package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private DishFlavorService dishFlavorService;


    /**
     * 新增菜品
     * @param dishDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){
        log.info(dishDto.toString());


        dishService.saveWithFlavor(dishDto);

        return R.success("新增菜品成功");

    }

    /*
    * 分页条件查询
    * */
    @GetMapping("/page")
    public R<Page<DishDto>> page(Integer page,Integer pageSize,String name) {
        //构造分页对象
        Page<Dish> pageInfo1 = new Page<>(page,pageSize);
        //构造条件对象
        QueryWrapper<Dish> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name),"name",name);
        queryWrapper.orderByAsc("sort").orderByDesc("update_time");

        dishService.page(pageInfo1,queryWrapper);

        //对 Page<Dish> pageInfo 做数据处理
        Page<DishDto> pageInfo2 = new Page<>(page,pageSize);

        // 1、 将page1 中的数据拷贝到page2 中 （分为两个阶段： List， Total）
        BeanUtils.copyProperties(pageInfo1,pageInfo2,"records");

        //2、将page1中的List<Dish> 转为 List<DishDto>
        List<DishDto> dishDtoList = new ArrayList<>();

        // 遍历page1中的list集合
        for (Dish dish : pageInfo1.getRecords()) {
            //将Dish转变为DishDto
            DishDto dishDto = new DishDto();
            // 将 dish 数据 拷贝到 dishDto
            BeanUtils.copyProperties(dish,dishDto);
            // 处理分类名称; (没判断 category 是否为空)
            Category category = categoryService.getById(dishDto.getCategoryId());

            dishDto.setCategoryName(category.getName());
            dishDtoList.add(dishDto);

        }

        pageInfo2.setRecords(dishDtoList);

        return  R.success(pageInfo2);

    }

    /**
     * 根据id查询菜品信息和对应的口味信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<DishDto> get(@PathVariable Long id){
        DishDto dishDto = dishService.getByIdWithFlavor(id);
        return R.success(dishDto);
    }

    /*
    * 修改菜品
    * */
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){

        dishService.updateByIdWithFlavor(dishDto);

        return R.success("修改成功");

    }

    /*
    * 根据条件查询对应的菜品数据
    *
    * */
    @GetMapping("list")
    public R<List<DishDto>> list(Dish dish){
        //构造查询条件
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(dish.getCategoryId() != null,Dish::getCategoryId,dish.getCategoryId());
        //添加条件，查询状态为1（起售状态）的菜品
        queryWrapper.eq(Dish::getStatus,1);
        //添加排序条件
        queryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);

        List<Dish> dishList = dishService.list(queryWrapper);

        //转换数据 将 List<Dish> 转换 List<DishDto>
        List<DishDto> dishDtoList = new ArrayList<>();

        //遍历dishesList
        for (Dish dish1 : dishList) {
            DishDto dishDto = new DishDto();
            // 吧Dish的数据拷贝到dishDto中
            BeanUtils.copyProperties(dish1,dishDto);

            //条件构造器
            QueryWrapper<DishFlavor> dishFlavorQueryWrapper = new QueryWrapper<>();
            dishFlavorQueryWrapper.eq("dish_id",dish1.getId());
            List<DishFlavor> dishFlavorList = dishFlavorService.list(dishFlavorQueryWrapper);
            //
            dishDto.setFlavors(dishFlavorList);

            //将dishDto 拷贝到 dishDtoList集合中
            dishDtoList.add(dishDto);

        }

        return R.success(dishDtoList);


    }


}
